import React from "react";
import axios from "../../api/axios";
import { Button, Checkbox, Form, Input } from "antd";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { username } from "../store/user/Index";
function Index() {
  const dispatch = useDispatch();
  const naviagte = useNavigate();
  const onFinish = (values) => {
    console.log("Success:", values);
    axios.post("/login", values).then((res) => {
      alert("登陆成功");
      localStorage.setItem("token", res.data.token);
      dispatch(username(res.data.token));
      localStorage.setItem("name", res.data.username);
      naviagte("/home");
    });
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };

  return (
    <div>
      <div
        style={{
          width: "400px",
          height: "500px",
          textAlign: "center",
          marginTop: "300px",
          marginLeft: "40%",
          backgroundColor: "rgb(126, 125, 125)",
          borderRadius: "15px",
        }}
      >
        <h2 style={{ textAlign: "center" }}>后台登陆</h2>
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 10 }}
          style={{ maxWidth: 400 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="账号"
            name="account"
            rules={[{ required: true, message: "Please input your username!" }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[
              { required: true, message: "Please input your password!" },
              {
                pattern: /^[A-Z a-z 0-9]{6,12}$/,
                message: "请输入6到12位",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item label={null}>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Index;
